const express = require("express");
const fs = require("fs");
const { imgPath } = require("../config");

const fileRouter = express.Router();

fileRouter.post("/upload", function (req, res) {
    console.log(req.files[0]); // 上传的文件信息
    // res.json({
    //     files:req.files
    // })

    const des_file = imgPath + "/" + req.files[0].originalname;
    fs.readFile(req.files[0].path, function (err, data) {
        fs.writeFile(des_file, data, function (err) {
            if (err) {
                console.log(err);
                res.json(err)
            } else {
                response = {
                    username:req.body.username,
                    message: "File uploaded successfully",
                    filename: req.files[0].originalname,
                };
            }
            console.log(response);
            res.json(response);
        });
    });

});

module.exports = fileRouter;
